﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shop.Core;
using Shop.Intermediate.Data;
using DoNet.Common.Utilities;

namespace Shop.WebApp.SysBase
{
    public partial class BasicDataManage : BasePage
    {
        private BasicTypeCore m_basicTypeCore;
        private BasicDataCore m_basicDataCore;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                m_basicTypeCore = new BasicTypeCore();
                m_basicDataCore = new BasicDataCore();

                if (!Page.IsPostBack)
                {
                    IEnumerable<IBasicType> query = m_basicTypeCore.GetList();
                    PageHelper.DropDownListBind<IBasicType>(ddlTypeCode, query, "Code", "Name",false);
                    IBasicType enType = query.FirstOrDefault();
                    lblType.Text = enType.Name;

                    gvBasicData.DataSource = enType.BasicDataEntity;
                    gvBasicData.DataBind();

                    BindTree(tvBasicData, query);
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void tvBasicData_SelectedNodeChanged(object sender, EventArgs e)
        {
            try
            {
                string code = tvBasicData.SelectedNode.Value;
                string name = tvBasicData.SelectedNode.Text;
                if (!code.Equals("0"))
                {
                    lblType.Text = name;
                    IBasicType en = m_basicTypeCore.GetEntity(code);
                    gvBasicData.DataSource = en.BasicDataEntity;
                    gvBasicData.DataBind();
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string typeCode = ddlTypeCode.SelectedValue;
                string message = string.Empty;
                IBasicData entity = m_basicDataCore.NewBasicData();
                FormHelper<IBasicData>.Instance.UpdateModel(OperateArea, entity);
                entity.BasicTypeEntity = m_basicTypeCore.GetEntity(typeCode);
                if (m_basicDataCore.Save(entity, ref message))
                {
                    gvBasicData.DataSource = m_basicTypeCore.GetEntity(typeCode).BasicDataEntity;
                    gvBasicData.DataBind();
                }
                MessageBox.Show(this.Page, message);
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnUpadate_Click(object sender, EventArgs e)
        {
            try
            {
                string typeCode = ddlTypeCode.SelectedValue;
                string message = string.Empty;
                IBasicData entity = m_basicDataCore.NewBasicData();
                FormHelper<IBasicData>.Instance.UpdateModel(OperateArea, entity);
                if (m_basicDataCore.Update(entity, ref message))
                {
                    gvBasicData.DataSource = m_basicTypeCore.GetEntity(typeCode).BasicDataEntity;
                    gvBasicData.DataBind();
                }
                MessageBox.Show(this.Page, message);

            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void gvBasicData_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                string code = gvBasicData.DataKeys[e.NewSelectedIndex].Values[0].ToString();
                this.Code.Text = code;
                this.Code.ReadOnly = true;
                IBasicData entity = m_basicDataCore.GetEntity(code);
                this.Name.Text = entity.Name;
                this.btnSave.Visible = false;
                this.btnUpadate.Visible = true;
                this.ddlTypeCode.SelectedValue = entity.BasicTypeEntity.Code;
                this.ddlTypeCode.Attributes["disabled"] = "disabled";
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        private void BindTree(TreeView tvBasicData, IEnumerable<IBasicType> query)
        {
            TreeNode topNode = new TreeNode("基础数据类型", "0");
            TreeNode currentNode;
            //TreeNode childNode;

            foreach (IBasicType a in query)
            {
                currentNode = new TreeNode(a.Name, a.Code);
                //foreach (IBasicData b in a.BasicDataEntity)
                //{
                //    childNode = new TreeNode(b.Name, b.Code);
                //    currentNode.ChildNodes.Add(childNode);
                //}
                //currentNode.Collapse();
                currentNode.SelectAction = TreeNodeSelectAction.Select;
                topNode.ChildNodes.Add(currentNode);
            }
            tvBasicData.Nodes.Add(topNode);
        }

        protected void lkbCreate_Click(object sender, EventArgs e)
        {
            this.btnSave.Visible = true;
            this.btnUpadate.Visible = false;
            this.ddlTypeCode.Attributes.Remove("disabled");
            this.ddlTypeCode.SelectedIndex = 0;
            this.Code.Text = string.Empty;
            this.Code.ReadOnly = false;
            this.Name.Text = string.Empty;
        }
    }
}
